package com.wing.cms.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.cms.dao.ChannelDao;
import com.wing.cms.domain.Channel;
import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Code;
/**
 * 
 * @author djl
 * 
 */
@Service
@Transactional
public class ChannelManager {
	@Autowired
	private ChannelDao channeldao;
	
	
	/**
	 * 获得该对象所有数据的列表
	 * 
	 * @return list
	 */
	public List<Map<String,Object>> findAll(){
		return channeldao.findAll();	 
	}
	
	public PageBean findPageList(int currentPage, int pageSize, String siteId, String pId){
		return channeldao.findPageList(currentPage,pageSize,siteId,pId);	 
	}
	
	/**
	 * 通过id得到对象
	 *
	 * @param  id
	 * @return pObject
	 */
	public Channel findById(String id){
		return channeldao.findById(id);	 
	}
	
	/**
	 * 通过siteId得到频道集合
	 *
	 * @param  siteId
	 * @return List
	 */
	public List<Map<String, Object>> findChannelsBySiteId(String siteId){
		return channeldao.findChannelsBySiteId(siteId);	 
	}
	
	/**
	 * 通过父id得到子对象集合
	 *
	 * @param  pid
	 * @return List
	 */
	public List<Map<String, Object>> findChildsByPid(String pid,String siteId){
		return channeldao.findChildByID(pid,siteId);	 
	}
	
	/**
	 * 保存对象
	 *
	 * @param  channel
	 */
	public void saveChannel(Channel channel){
		if(channel.getId() == null ||  "".equals(channel.getId())){
			channel.setId(new GUID().toString());
			channeldao.insert(channel);
		}else{
			channeldao.update(channel);
		}
	}

	/**
	 * 编辑对象
	 *
	 * @param  channel
	 */
	public void updateChannel(Channel channel){
		channeldao.update(channel);
	}
	
	 /**
	 * 删除对象
	 *
	 * @param  chk 对象id数组
	 */
	public void deleteChannels(String[] chk) {
		for(int i=0;i<chk.length;i++){
			deleteChannel(chk[i]);
		}
	}
	
	/**
	 * 递归删除其下面的子频道
	 * @param artgroupId
	 */
	public void deleteChannel(String chanId){
		List<Map<String, Object>> list = channeldao.findChildByID(chanId);
		for(int i=0;i<list.size();i++){
			Map map = list.get(i);
			deleteChannel((String)map.get("id"));
		}
		channeldao.delete(chanId);
	}

	/********************************获取频道树的xml开始*******************************
	 * @param siteId 
	 * @param  */
	
	public String createDHTMLXML(String parentid,boolean firstFlag, String siteId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		if (parentid.equalsIgnoreCase(Code.CODEROOT) || firstFlag) {
			Channel rootChannel = channeldao.findById(parentid);
			buffer.append("<tree id=\"0\">");
			buffer.append("<item text=\""+rootChannel.getChannel_name()+"\" id=\""+rootChannel.getId()+"\" open=\"1\" select=\"1\">");
		} else {
			buffer.append("<tree id=\"" + parentid + "\">");
		}
		// 循环读取所有节点 
		List<Map<String, Object>> list = channeldao.findChildByID(parentid,siteId);
		for (Map map : list) {
			String id = (String)map.get("id");
			String name = (String)map.get("channel_name");
			buffer.append("<item text= \"" + name + "\"");
			buffer.append(" id= \"" + id + "\"");

			if (codeIsLeaf(id,siteId)) {
				buffer.append(" im0=\"iconText.gif\" im1=\"iconTexts.gif\" im2=\"iconText.gif\"");
				buffer.append(" child=\"0\" >");
			} else {
				buffer.append(" im0=\"folderClosed.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\"");
				buffer.append(" child=\"" + id + "\">");
			}
			buffer.append("</item>");
		}
		if (parentid.equalsIgnoreCase(Code.CODEROOT) || firstFlag) {
			buffer.append("</item>");
		}
		buffer.append("</tree>");
		return buffer.toString();
	}
	

	/**
	 * 判断该编码是否是叶子节点
	 * @param codeanization Id
	 * @return 
	 */
	private boolean codeIsLeaf(String id,String siteId){
		List list = channeldao.findChildByID(id,siteId);
		return list.size()<=0;
	}
	/********************************获取频道树的xml结束********************************/

	public String findLayoutFileById(String id){
		return channeldao.findLayoutFileById(id);	 
	}
	
	public String findSitePathById(String id){
		return channeldao.findSitePathById(id);	 
	}

	
	/**
	 *复制频道	 *
	 * @param  channel
	 */
	public void cloneChannel(String id){
		channeldao.cloneChannel(id);
	}
	
	public void uptemp(String id) {
		channeldao.uptemp(id);	
	}
}



